<script>
  import sheep from '@/sheep';
  import dayjs from 'dayjs';

  export default {
    name: 's-update-popup',
    data() {
      return {};
    },
    mounted() {
      // #ifndef MP-WEIXIN

      // #endif
    },
    computed: {
      IMG_URL() {
        return sheep.$store('AI').sq_image_path;
      },
      versionInformation() {
        return sheep.$store('AI').versionInformation;
      },
      systemInfo() {
        return sheep.$platform.device;
      },
    },
    watch: {
      versionInformation: {
        handler(newValue, oldValue) {
          console.log('更新信息=>',!!newValue && newValue != {});
          this.$nextTick(() => {
            if (!!newValue && newValue != {}) {
              console.log('获取到更新信息');
              this.openPopup();
            }
          });
        },
        immediate: true,
        deep: true,
      },
    },
    methods: {
      dayjs,
      openPopup() {
        this.$refs.updatePopup.open();
      },
      updateNow() {
        // #ifdef MP-WEIXIN
        // const updateManager = uni.getUpdateManager();
        // updateManager.onCheckForUpdate((res) => {
        //   if (res.hasUpdate) {
        //     updateManager.applyUpdate();
        //     this.closePopup();
        //   } else {
        //     this.closePopup();
        //     sheep.$helper.toast('当前已是最新版本');
        //   }
        // });
        // updateManager.onUpdateFailed((res) => {
        //   // 新的版本下载失败
        //   console.log('download error');
        //   uni.showModal({
        //     title: '提示',
        //     content: '新版小程序下载失败\n请自行退出程序，手动卸载本程序，再运行',
        //     confirmText: '知道了',
        //   });
        // });
        // #endif
        // #ifdef APP-PLUS
        if (this.systemInfo.platform.toLocaleLowerCase() == 'ios') {
          console.log('打开ios');
          plus.runtime.launchApplication({
            action: `itms-apps://itunes.apple.com/cn/app/id${this.versionInformation.url}`,
          });
        }
        if (this.systemInfo.platform.toLocaleLowerCase() == 'android') {
          console.log(this.versionInformation.url);
          const downloadTask = uni.downloadFile({ //执行下载
            url: this.versionInformation.url, //下载地址
            success: downloadResult => { //下载成功
              console.log(downloadResult);
              if (downloadResult.statusCode == 200) {
                plus.runtime.install( //安装
                  downloadResult.tempFilePath, {
                    force: true,
                  },
                  function(res) {
                    console.log(res);
                    plus.runtime.restart();
                  },
                  function(err) {
                    console.log(err, '更新失败');
                  },
                );
              }
            },
            fail: (err) => {
              console.log(err);
            },
          });
          let showLoading = plus.nativeUI.showWaiting('正在下载', {
            back: 'none',
          });
          downloadTask.onProgressUpdate((res) => {
            console.log(res);
            showLoading.setTitle('正在下载' + res.progress + '%  ');
            if (res.progress == 100) {
              plus.nativeUI.closeWaiting();
            }
          });
        }
        // #endif
      },
      closePopup() {
        this.$refs.updatePopup.close();
      },
    },
  };
</script>

<template>
  <uni-popup ref="updatePopup" :maskClick="false" :maskBackgroundColor="'rgba(0,0,0,0.6)'">
    <view class="wh_574 bg_FFFFFF flex flex-column align-center"
          style="border-radius: 24rpx;overflow: hidden;position: relative;"
          v-if="!!versionInformation"
    >
      <view v-if="versionInformation.update_type != 3"
            style="position: absolute;right: 20rpx;top: 20rpx;z-index:10;"
            @click.stop="closePopup"
      >
        <image :src="`${IMG_URL}/index/update_close.png`"
               class="wh_32 hW_32"
        ></image>
      </view>
      <view class="wh_574 hW_241">
        <image class="wh_574 hW_241" :src="`${IMG_URL}/index/update_header.png`"></image>
      </view>
      <view class="wh_574 hW_103 title flex align-center justify-center fs_30 fw_bold color_292929">
        <text>升级到最新版本:</text>
        <text class="fs_26">v{{ versionInformation.version }}</text>
      </view>
      <scroll-view scroll-y
                   class="wh_456 hW_250"
      >
        <mp-html :content="versionInformation.descrition"></mp-html>
      </scroll-view>
      <view class="mt_30 wh_508 hW_90 flex align-center justify-center fs_30 fw_500 color_FFFFFF bg_FF4206"
            style="border-radius: 50rpx;"
            @click.stop="updateNow"
      >
        立即更新
      </view>
      <view class="fs_25 fw_500"
            style="color: #959595;"
      >
        发布时间: {{ dayjs(versionInformation.createtime * 1000).format('YYYY年MM月DD日 HH:mm') }}
      </view>
      <view class="hW_30"></view>
    </view>
  </uni-popup>
</template>

<style scoped lang="scss">

</style>